const std = @import("std");

pub fn build(b: *std.Build) void {
    // 标准构建选项
    const optimize = b.standardOptimizeOption(.{});
    const target = b.standardTargetOptions(.{});
    
    // Yaya libOS库配置
    const lib = b.addStaticLibrary(.{
        .name = "yaya",
        .root_source_file = .{ .path = "src/lib.zig" },
        .target = target,
        .optimize = optimize,
    });
    
    // 链接标准库（在no_std模式下可选）
    lib.linkLibC();
    
    // 构建选项
    const build_options = b.addOptions();
    build_options.addOption(bool, "enable_std", true);
    build_options.addOption(bool, "enable_threading", true);
    build_options.addOption(bool, "enable_networking", true);
    build_options.addOption(bool, "enable_graphics", false);
    
    lib.addOptions("build_options", build_options);
    
    // 安装库文件
    b.installArtifact(lib);
    
    // 示例应用构建
    const examples = [_][]const u8{
        "examples/hello_world/main.zig",
        "examples/minimal_app/main.zig",
        "examples/network_app/main.zig",
    };
    
    for (examples) |example_path| {
        const example_name = std.fs.path.stem(std.fs.path.basename(example_path));
        const exe = b.addExecutable(.{
            .name = example_name,
            .root_source_file = .{ .path = example_path },
            .target = target,
            .optimize = optimize,
        });
        
        // 链接Yaya库
        exe.linkLibrary(lib);
        exe.linkLibC();
        
        b.installArtifact(exe);
        
        // 运行命令
        const run_cmd = b.addRunArtifact(exe);
        run_cmd.step.dependOn(b.getInstallStep());
        
        const run_step = b.step(b.fmt("run-{s}", .{example_name}), 
            b.fmt("运行{s}示例", .{example_name}));
        run_step.dependOn(&run_cmd.step);
    }
    
    // 单元测试
    const unit_tests = b.addTest(.{
        .root_source_file = .{ .path = "src/lib.zig" },
        .target = target,
        .optimize = optimize,
    });
    
    unit_tests.addOptions("build_options", build_options);
    
    const run_unit_tests = b.addRunArtifact(unit_tests);
    const test_step = b.step("test", "运行单元测试");
    test_step.dependOn(&run_unit_tests.step);
    
    // 基准测试
    const bench_tests = b.addTest(.{
        .root_source_file = .{ .path = "src/benchmarks.zig" },
        .target = target,
        .optimize = .ReleaseFast,
    });
    
    bench_tests.addOptions("build_options", build_options);
    
    const run_bench_tests = b.addRunArtifact(bench_tests);
    const bench_step = b.step("bench", "运行性能基准测试");
    bench_step.dependOn(&run_bench_tests.step);
}